<template>
  <div>
    <van-tabbar
      v-model="active"
      active-color="rgb(12,52,186)"
      inactive-color="rgb(100,101,102)"
      route
    >
      <van-tabbar-item
        v-for="(item, index) in tabberData"
        :key="index"
        :badge="item.badge"
        :dot="item.dot"
        :to="item.to"
      >
        <span>{{ item.title }}</span>
        <template #icon="props">
          <img
            :src="props.active ? item.icon.activeIcon : item.icon.inactiveIcon"
          />
        </template>
      </van-tabbar-item>
    </van-tabbar>
    <div>
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
export default {
  name: "Main",
  data() {
    return {
      active: 0,
    };
  },
  computed:{
    tabberData(){
      return[
        {
          title: "首页",
          icon: {
            activeIcon: require("../assets/images/home_active.png"),
            inactiveIcon: require("../assets/images/home.png"),
          },
          to: { name: "Home" },
        },
        {
          title: "菜单",
          icon: {
            activeIcon: require("../assets/images/menu_active.png"),
            inactiveIcon: require("../assets/images/menu.png"),
          },
          dot: false,
          to: { name: "Menu" },
        },
        {
          title: "购物袋",
          icon: {
            activeIcon: require("../assets/images/shopbag_active.png"),
            inactiveIcon: require("../assets/images/shopbag.png"),
          },
          badge: this.$root.publicShopbagCount,
          to: { name: "Shopbag" },
        },
        {
          title: "我的",
          icon: {
            activeIcon: require("../assets/images/my_active.png"),
            inactiveIcon: require("../assets/images/my.png"),
          },
          to: { name: "My" },
        },
      ]
    }
  },
    created() {
    //获取根组件的data数据
    // if (this.$root.publicShopbagCount === '') {
      this.getShopbagCount();
    // }
    
  },

  methods: {
    //如果用户已经登录，则查询购物袋的商品总数量
    getShopbagCount() {
      //获取token
      let tokenString = this.$cookies.get("TOKEN");

      this.axios({
        method: "GET",
        url: "/shopcartCount",
        params: {
          tokenString,
        },
      })
        .then((res) => {
          if (res.data.code === 4000) {
            
            
            this.$root.publicShopbagCount = res.data.result;
            // console.log(res.data.result);
          }
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>

<style lang="less" scoped>
</style>